import { Form } from 'antd-mini/Form/form';
import { login } from "../../api/common/index"
import ur from "../../utils/extendApi"
import { baseBehavior } from "./behaviors"
//test.js
let leftHeight = 0, rightHeight = 0; //分别定义左右两边的高度
let query;
Page({
  behaviors: [baseBehavior],
  data: {
    position: 'horizontal',
    form: '',
    formRefList: '',
    swipperList: [{
      url: "https://ufm-pic.ur.com.cn/image/tmp/1699261737609.jpg"
    }, {
      url: "https://ufm-pic.ur.com.cn/image/tmp/17104877690891710487769064.jpg"
    }, {
      url: "https://ufm-pic.ur.com.cn/image/tmp/17104877690891710487769064.jpg"
    }, {
      url: "https://ufm-pic.ur.com.cn/image/tmp/17104877690891710487769064.jpg"
    }, {
      url: "https://ufm-pic.ur.com.cn/image/tmp/17104877690891710487769064.jpg"
    }],
    list: [{
      title: '1',
      url: 'http://zq.jhcms.cn/attachs/photo/201711/20171130_176CFE51B6710715B1BBBEF2F86ACB0C.jpg',
    }, {
      title: '2',
      url: 'http://img3.imgtn.bdimg.com/it/u=1417732605,3777474040&fm=26&gp=0.jpg',
    }, {
      title: '3',
      url: 'http://f10.baidu.com/it/u=121654667,1482133440&fm=72',
    }, {
      title: '4',
      url: 'http://img3.imgtn.bdimg.com/it/u=1417732605,3777474040&fm=26&gp=0.jpg',
    }, {
      title: '5',
      url: 'http://zq.jhcms.cn/attachs/photo/201711/20171130_9E39DA252E3946BE36218D85876C4AB4.jpg',
    }, {
      title: '6',
      url: 'http://img3.imgtn.bdimg.com/it/u=1417732605,3777474040&fm=26&gp=0.jpg',
    }, {
      title: '7',
      url: 'http://img3.imgtn.bdimg.com/it/u=1417732605,3777474040&fm=26&gp=0.jpg'
    }, {
      title: '8',
      url: 'http://img3.imgtn.bdimg.com/it/u=1417732605,3777474040&fm=26&gp=0.jpg',
    }, {
      title: '9',
      url: 'http://f10.baidu.com/it/u=121654667,1482133440&fm=72'
    }, {
      title: '7',
      url: 'http://img4.imgtn.bdimg.com/it/u=2748975304,2710656664&fm=26&gp=0.jpg'
    }, {
      title: '8',
      url: 'http://img2.imgtn.bdimg.com/it/u=1561660534,130168102&fm=26&gp=0.jpg'
    }, {
      title: '9',
      url: 'http://img3.imgtn.bdimg.com/it/u=1417732605,3777474040&fm=26&gp=0.jpg'
    }, {
      title: '10',
      url: 'http://zq.jhcms.cn/attachs/photo/201711/20171130_176CFE51B6710715B1BBBEF2F86ACB0C.jpg',
    }],
    leftList: [] as any,
    rightList: []as any
  },
  onShow() {
    if (this.data.token) {

      wx.showLoading({
        title: '自动登录中...'
      })

      setTimeout(() => {
        wx.switchTab({
          url: '/pages/home/index'
        })
      }, 500);
    }
  },
  onLoad() {
    this.isLeft();

    this.form = new Form({
      rules: {
        username: [
          {
              required: true,
              message: '需要输入密码',
          },
      ],
          password: [
              {
                  required: true,
                  message: '需要输入密码',
              },
          ],
      },
  });
  if (this.formRefList) {
      this.formRefList.forEach((ref) => {
          this.form.addItem(ref);
      });
  }
  },

  async isLeft() {
    const { list, leftList, rightList } = this.data;
    query = wx.createSelectorQuery();
    for (const item of list as any) {
      leftHeight <= rightHeight ? leftList.push(item) : rightList.push(item); //判断两边高度，来觉得添加到那边
      await this.getBoxHeight(leftList, rightList,query);
    }
  },

  getBoxHeight(leftList:any, rightList:any,query:any) { 
    //获取左右两边高度
    return new Promise((resolve:any, reject:any) => {
      this.setData({ leftList, rightList }, () => {
        query.select('#left').boundingClientRect();
        query.select('#right').boundingClientRect();
        query.exec((res:any) => {
          leftHeight = res[0].height; //获取左边列表的高度
          rightHeight = res[1].height; //获取右边列表的高度
          resolve();
        });
      });
    })
  },
  handleRef(ref:any) {
    if (!this.formRefList) {
      this.formRefList = [];
    }
    this.formRefList.push(ref.detail);
  },

  reset() {
    // this.form.reset();
  },

  toggle() {
    this.setData({
      position: this.data.position === 'horizontal' ? 'vertical' : 'horizontal',
    });
  },

  async submit() {
    // 表单校验
    const values = await this.form.submit()
    // 登录请求
    const res: any = await login(values)
    // 拿到接口数据进行处理
    const token: any = res.data.token
    const currentUserInfo: any = {
      ...res.data,
      token: undefined
    }
    // 把数据存入状态管理
    this.setToken(token)
    this.setCurrentUserInfo(currentUserInfo)
    // 把数据存入本地存储
    ur.setStorageApi('token', token)
    ur.setStorageApi('currentUserInfo', currentUserInfo)
    // 登录成功提示
    ur.okToastApi('登录成功！')
    // 登录跳转
    wx.switchTab({
      url: '/pages/home/index'
    })
  }
});
